package cn.jsc.pet.controller;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.util.AjaxResult;
import cn.jsc.basic.util.PageList;
import cn.jsc.pet.domain.Pet;
import cn.jsc.pet.domain.SearchMasterMsg;
import cn.jsc.pet.query.SearchMasterMsgQuery;
import cn.jsc.pet.service.ISearchMasterMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "寻主接口",description = "寻主接口详细描述")
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    //获取所有对象：get
    //Get http://localhost:8080/searchMasterMsg
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.getAll();
    }

    /**
     * 发布寻主信息
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @ApiOperation(value = "发布寻主消息",notes = "")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布失败，"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，"+e.getMessage());
        }
    }

    /**
     * 处理寻主消息
     * @param pet
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet){
        try {
            searchMasterMsgService.handle(pet);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布失败，"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，"+e.getMessage());
        }
    }



    /**
     * 管理员查询待处理消息
     * @param query
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return  searchMasterMsgService.pending(query,request);
    }
    /**
     * 管理员查询已处理消息
     * @param query
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return  searchMasterMsgService.processed(query,request);
    }

    /**
     * 获取用户的寻主消息
     * @param query
     * @return
     */
    @PostMapping("/userMsg")
    public PageList<SearchMasterMsg> userMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return searchMasterMsgService.userMsg(query,request);
    }


}
